import logging

from core import shopping
from db.db_handler import read
from interface import bank_interface
from lib.common import is_positive_num


def check_balance(user_dict: dict):
    """查看余额"""
    # 调用查看余额接口
    bank_interface.check_balance_interface(user_dict)


def recharge(user_dict: dict):
    """充值功能"""
    while True:
        amount = input('请输入充值的金额:').strip()
        # 输入金额是否为大于等于0的正数
        if not is_positive_num(amount):
            print('必须输入大于等于0的数字，请重新输入！')
            continue
        # 调用充值接口
        bank_interface.recharge_interface(user_dict, amount)
        return


def withdraw(user_dict: dict):
    """余额提现"""
    while True:
        # 打印余额
        bank_interface.check_balance_interface(user_dict)

        amount = input('请输入要提现的金额（手续费5%）:').strip()
        # 输入金额是否为大于等于0的正数
        if not is_positive_num(amount):
            print('必须输入大于等于0的数字，请重新输入！')
            continue
        # 统计提现金额+手续费
        total_amount = float(amount) + float(amount) * 0.05
        if user_dict['balance'] < total_amount:
            print(f'账户余额不足{amount}或提现后余额不足5%手续费，请重新输入！')
            continue
        # 调用提现接口
        bank_interface.withdraw_interface(user_dict, total_amount, float(amount))
        return


def transfer(user_dict: dict):
    """转账功能"""
    # 输入目标用户名，获取目标数据
    target_username = input('请输入转账目标用户:').strip()
    target_username_dict = read(target_username)
    # 转账目标为自己
    if target_username == user_dict['username']:
        print('无法给自己转账！')
        return
    # 用户不存在
    if not target_username_dict:
        print('该用户不存在或异常，无法使用转账！')
        return
    while True:
        # 打印余额
        bank_interface.check_balance_interface(user_dict)

        amount = input('请输入转账金额:').strip()
        if not is_positive_num(amount):
            print('必须输入大于等于0的数字，请重新输入！')
            continue
        if user_dict['balance'] < float(amount):
            print(f'账户余额不足{amount}，请重新输入！')
            continue
        # 调用转账接口
        bank_interface.transfer_interface(user_dict, target_username_dict, float(amount))
        return


def check_flow(user_dict: dict):
    """查看流水"""
    # 调用查看流水功能
    bank_interface.check_flow_interface(user_dict)


src_dict = {
    '1': check_balance,
    '2': recharge,
    '3': withdraw,
    '4': transfer,
    '5': check_flow,
    '6': shopping.run,  # 进入购物车功能
}


def run(user_dict):
    while True:
        print("欢迎使用 ATM + 购物车 系统".center(40, '*'))
        print("1.查看余额".center(40))
        print("2.充值功能".center(40))
        print("3.余额提现".center(40))
        print("4.转账功能".center(40))
        print("5.查看流水".center(40))
        print("6.购物功能".center(40))
        print(f" 当前用户:{user_dict['username']}  当前余额: {user_dict['balance']} ".center(40, '*'))

        order = input('请选择功能（输入q退出系统 logout退出登录）:').strip()

        if order == 'logout':
            print(f"{user_dict['username']}退出登录")
            logging.getLogger(__name__).info(f"{user_dict['username']}退出登录")
            return

        if order == 'q':
            print('退出系统！')
            exit(0)

        if not src_dict.get(order):
            print('输入的选项不存在，请重新输入！')
            continue
        src_dict[order](user_dict)

        input('按回车继续...')
